package com.map.test;

import com.map.model.StudentModel;

import java.util.ArrayList;

/**
 * @author Li
 * @Date 2023/4/14 10:56
 */
public class FilterArrayList {
    public static void main(String[] args) {
        /**
         * 元素筛选，将年龄小于18的学生找出并存入新的集合对象
         */
        ArrayList<StudentModel> studentModelArrayList = new ArrayList<>();
        studentModelArrayList.add(new StudentModel("小明", 20));
        studentModelArrayList.add(new StudentModel("小红", 15));
        studentModelArrayList.add(new StudentModel("小丽", 17));
        studentModelArrayList.add(new StudentModel("小华", 22));
        studentModelArrayList.add(new StudentModel("小强", 16));
        studentModelArrayList.add(new StudentModel("小兵", 23));
       ArrayList<StudentModel> filter = filter(studentModelArrayList);
        System.out.println(filter);
    }

    private static ArrayList<StudentModel> filter(ArrayList<StudentModel> studentModelArrayList) {
        ArrayList<StudentModel> list = new ArrayList<>();
        for (int i = 0; i < studentModelArrayList.size(); i++) {
            if (studentModelArrayList.get(i).getAge()<18){
                list.add(studentModelArrayList.get(i));
            }
        }

        return list;
    }
}
